﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace AspnetCore03.Middleware
{
    public class TestMiddleware
    {
        private readonly RequestDelegate _next;

        public TestMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        //.netcore的管道会调用该方法
        public async Task InvokeAsync(HttpContext httpContext)
        {
            //在这里写中间件的业务代码!!

            //_next之前:HTTP请求部分的处理

            //这里只写业务代码,那么当前中间件就是一个终端中间件
            //如果要继续调用后面的中间件需要使用:_next
            await _next(httpContext);
            //await _next.Invoke(httpContext);

            //_next之后:HTTP响应部分的处理
        }
    }

    public static class TestMiddlewareExtensions
    {
        public static IApplicationBuilder UseTest(this IApplicationBuilder app)
        {
            return app.UseMiddleware<TestMiddleware>();
        }
    }


}
